package com.yummy.controller;

import com.github.pagehelper.PageInfo;
import com.yummy.common.BaseResponse;
import com.yummy.common.TokenUtils;
import com.yummy.dto.QuerySetmealVo;
import com.yummy.dto.SetmealDto;
import com.yummy.entity.Setmeal;
import com.yummy.service.SetmealService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    TokenUtils tokenUtils;

    @Autowired
    private RedisTemplate redisTemplate;

    Logger logger = LoggerFactory.getLogger(SetmealController.class);

    /**
     * 新增套餐
     */
    @PostMapping("/addSetmeal")
    BaseResponse addSetmeal(@RequestBody SetmealDto setmealDto) {

//        setmealDto.setCategoryId(1397844263642378242L);
        setmealService.addSetmeal(setmealDto);
        return BaseResponse.success();
    }


    @PostMapping("/queryAll")
    BaseResponse querySetmeal(@RequestBody QuerySetmealVo querySetmealVo) {

        logger.info("querySetmeal");

        PageInfo<SetmealDto> queryDishVoPageInfo = setmealService.queryByPageAndCondition(querySetmealVo);


        //根据Dish 中的 "categoryId": 1397844263642378242,找到具体的

        return BaseResponse.success().data("pageInfo", queryDishVoPageInfo);

    }

    @GetMapping("/remove")
    public BaseResponse removeSetmeal(@RequestParam String[] ids) {
        System.out.println("ids==>" + ids);
        for (String id : ids) {
            System.out.println(id);
        }
        boolean result = setmealService.removeSetmeals(ids);
        if (result) {
            return BaseResponse.success().message("删除成功");
        } else {
            return BaseResponse.fail().message("删除失败");
        }
    }

    @GetMapping("/findSetmealById")
    public Setmeal findSetmealById(@RequestBody String id) {
        Setmeal setmeal = setmealService.findSetmealById(id);
        return setmeal;
    }


    @GetMapping("/setStatus/{id}/{status}")
    public BaseResponse setStatus(@PathVariable("id") String id, @PathVariable("status") String status) {
        System.out.println(id + "----" + status);
        boolean result = setmealService.updateStatus(id, status);
        if (result) {
            return BaseResponse.success();
        }
        return BaseResponse.fail().data("status", setmealService.findSetmealById(id).getStatus());
    }
}
